package com.zmn.oms.task.job.third.zhimi;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.dto.ResultDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.manager.lts.interfaces.task.TaskManager;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.third.zhimi.ZhimiConsts;
import com.zmn.oms.third.zhimi.service.ZhimiOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;

/**
 * 类描述：定时拉取智米订单信息任务
 *
 * @author LiangHailong
 * @date 2019/09/07 18:38
 */
@LTS
@Slf4j
public class ZhimiOrderJob {
    @Autowired
    ZhimiOrderService zhimiOrderService;

    @Resource
    TaskManager taskManager;

    /**
     * 任务一：每隔15分钟，拉取一次智米订单
     * 任务二：每天7点-20点，每小时拉取一次配件物流信息
     */
    @PostConstruct
    void initJob() {
        // 任务一：每隔15分钟，拉取一次智米订单
        try {
            Job job = new Job();
            job.setTaskId(TaskConsts.TASK_ID_ZHIMI_PULL_ORDER);
            job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_ZHIMI_PULL_ORDER);
            job.setCronExpression("0 /15 * * * ?");
            job.setReplaceOnExist(true);
            ResultDTO result = taskManager.submitTask(job);
            if (!result.getStatusBool()) {
                log.error("【{}】 Pull orders job submit fail...", ZhimiConsts.CHANNEL_NAME);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 任务二：每天7点-20点，每小时拉取一次配件物流信息
        try {
            Job job = new Job();
            job.setTaskId(TaskConsts.TASK_ID_ZHIMI_PULL_APPLIED_PART_EXPRESS);
            job.setParam(TaskManager.SHARD_FIELD, TaskConsts.TASK_ID_ZHIMI_PULL_APPLIED_PART_EXPRESS);
            job.setCronExpression("0 0 7-20 * * ?");
            job.setReplaceOnExist(true);
            ResultDTO result = taskManager.submitTask(job);
            if (!result.getStatusBool()) {
                log.error("【{}】 Pull applied part express job submit fail...", ZhimiConsts.CHANNEL_NAME);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 拉取智米订单
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_ZHIMI_PULL_ORDER)
    public Result runPullOrdersJob(Job job) {
        if (!ZhimiConsts.POWER_ON) {
            log.info("【{}】<订单拉取> 该功能没有开启。", ZhimiConsts.CHANNEL_NAME);
            return null;
        }

        try {
            zhimiOrderService.pullOrders();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        log.info("【{}】拉取智米订单任务执行成功 ！", ZhimiConsts.CHANNEL_NAME);
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }

    /**
     * 拉取智米配件物流信息
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_ZHIMI_PULL_APPLIED_PART_EXPRESS)
    public Result runPullAppliedPartExpressJob(Job job) {
        if (!ZhimiConsts.POWER_ON) {
            log.info("【{}】<配件物流信息拉取> 该功能没有开启。", ZhimiConsts.CHANNEL_NAME);
            return null;
        }

        try {
            // 查询两个月内的订单
            Date today = DateUtil.parse(DateUtil.toString(new Date(), DateUtil.FORMAT_DATE), DateUtil.FORMAT_DATE);
            Date startTime = DateUtil.addDays(today, -60);
            zhimiOrderService.pullAppliedPartExpress(startTime);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        log.info("【{}】拉取智米配件物流信息任务执行成功 ！", ZhimiConsts.CHANNEL_NAME);
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
